 const { ipcRenderer } = require("electron");
const { readdirSync, statSync } = require("node:fs");
window.ipcRenderer = ipcRenderer;

// index.html
window.alwaysOnTop = () => {
  ipcRenderer.sendTo(window.senderId, "win", "alwaysOnTop");
  let data = window.getData()
  window.sendSaveData(data)
};

window.isAlwaysOnTop = () => {
  ipcRenderer.sendTo(window.senderId, "win", "isAlwaysOnTop");
};

window.getPosition = () => {
  ipcRenderer.sendTo(window.senderId, "win", "position");
};

window.moveDirection = (type, num) => {
  ipcRenderer.sendTo(window.senderId, "direction", {type, num});
}

window.sendSaveData = (data) => {
  ipcRenderer.sendTo(window.senderId, "saveData", data);
};



window.getIcons = (url) => {
  let list = readdirSync(url)
  const icons = list.map((item) => {
    return {
      icon: utools.getFileIcon(`${url}/${item}`),
      url: `${url}/${item}`,
      name: item
    }
  })

  return icons;
}


window.isValidUrl = (url) => {
  try {
    statSync(url);
    return true;
  } catch (e) {
    return false;
  }
}


// 公共
ipcRenderer.on("senderId", (event) => {
  window.senderId = event.senderId;
});

window.closeWin = () => {
  if(window.closeSettingWin){
    window.closeSettingWin()
  }
  ipcRenderer.sendTo(window.senderId, "win", "close");
};

// 向父页面传送数据
window.toParentPage = (data, info) => {
  ipcRenderer.sendTo(window.senderId, 'toParent', {data, ...info});
}
